<?php
/**
 * Abstract class for database access in model
 *
 * @package Opt_Model
 * @author Adam Brodziak adam.brodziak@rapide.pl
 * @since 0.4dev rev 83
 */
abstract class Opt_Model
{
	private
		$_oDatabase,
		$_aDatabaseTableNames = array(),
		$_aParameters = array();

		
	/**
	 * Simple access to database class
	 *
	 * @return object Database object handler
	 */
	public function getDB()
	{
		if (!isset($this->_aParameters['Database']))
		{
			$this->_aParameters = Rapide_Config_Loader_PhpArray::load('Database.php');
			$sClass = $this->getDatabaseClassName();
			$this->_oDatabase = new $sClass($this->_aParameters['Database']);
		}
		return $this->_oDatabase;
	}

	/**
	 * Get table name for Database
	 *
	 * @param string Table name
	 * @return string Mapped table name, or given name if not mapped
	 */
	public function t($sTable)
	{
		if (!isset($this->_aDatabaseTableNames['Tables']))
		{
			$this->_aDatabaseTableNames = Rapide_Config_Loader_PhpArray::load('Tables.php');
		}
		return (!empty($this->_aDatabaseTableNames['Tables'][$sTable])) ?
		$this->_aDatabaseTableNames['Tables'][$sTable] : $sTable;
	}

	/**
	 *	Quote data
	 *
	 * @param mixed Variable to quote
	 * @return string Quoted variable
	 */
	public function q($mValue)
	{
		return '\'' . $mValue . '\'';
	}

	/**
	 *	Quote array's data
	 *
	 * @param array Array witch have fields to quote
	 * @return array Quoted array
	 */
	public function qA(array $aArray)
	{
	 $aQuotedArray = array();

	 foreach($aArray as $mKey => $mValue)
		$aQuotedArray['`'.$mKey.'`'] = $this->q($mValue);

	 return $aQuotedArray;
	}

	/**
	 * Gets database class name
	 *
	 * This function allows to create concrete database object. Child class must'
	 * inherits this method, and return database specific class name.
	 *
	 * @return string Database class name
	 */
	protected abstract function getDatabaseClassName();
}
?>